
package com.xebest.web.controller.restapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xebest.api.dto.ErpVenInquirysheetDTO;
import com.xebest.api.dto.XeVeInquireSheetRequestDTO;
import com.xebest.api.dto.XeVeInquireSheetResponseDTO;
import com.xebest.api.dto.XeVenInquirySheetInfoDTO;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.dto.BaseResponseDTO;
import com.xebest.constant.XeErrorCode;
import com.xebest.web.form.XeVenInquirySheetForm;
import com.xebest.web.model.XeVenInquirySheetEntity;
import com.xebest.web.service.XeVenInquirysheetService;
import com.xebest.web.view.XeVenInquirySheetView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 询价单同步接口
 *
 * @author 李贝贝
 */
@RestController
@RequestMapping("restapi/quiry")
public class XeVenInquirySheetRestApi extends BaseController {

    private static Logger log = LoggerFactory.getLogger(XeVenInquirySheetRestApi.class);

    @Autowired
    private XeVenInquirysheetService xeVenInquirysheetService; //询价单业务操作

    /**
     * 询价单同步接口
     *
     * @author 李贝贝
     */
    @RequestMapping("/saveQuirySheet")
    public BaseResponseDTO insertInquirySheet(@RequestBody ErpVenInquirysheetDTO iDto) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        List<XeVenInquirySheetInfoDTO> sheetListDto = iDto.getListDto();
        String incomParamJson = JSONObject.toJSONString(sheetListDto);
        log.info("询报价订单同步接口传入参数： {}", incomParamJson);
        //1、验证参数
        //2、拼装参数
        List<XeVenInquirySheetEntity> sheetInfoList = JSONArray.parseArray(incomParamJson, XeVenInquirySheetEntity.class);
        log.info("鲜易云ERP订单同步接口保存实体为参数： {}", JSONObject.toJSONString(sheetInfoList));
        //3、返回结果
        //log.debug("询报价订单同步接口保存实体为：{}",JSONObject.toJSONString(sheetInfoList));
        //根据询价单号查询是否有这条询价单
        try {
            int result = xeVenInquirysheetService.batchInsertInquirySheet(sheetInfoList);
            if (result > 0) {
                responseDTO.setMsg("新增询价单成功");
                responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
            }
        } catch (Exception e) {
            log.info("新增询价单异常", e);
            responseDTO.setMsg("新增询价单异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return responseDTO;
    }


    /**
     * 询价单同步  批量修改
     *
     * @author Wangyp
     */
    @RequestMapping("/batchUpdateInquiry")
    public BaseResponseDTO batchUpdateInquiry(@RequestBody ErpVenInquirysheetDTO iDto) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        List<XeVenInquirySheetInfoDTO> sheetListDto = iDto.getListDto();
        String incomParamJson = JSONObject.toJSONString(sheetListDto);
        log.info("询报价订单同步修改接口传入参数： {}", incomParamJson);
        //1、验证参数
        //2、拼装参数
        List<XeVenInquirySheetEntity> sheetInfoList = JSONArray.parseArray(incomParamJson, XeVenInquirySheetEntity.class);
        log.info("鲜易云ERP订单同步修改接口保存实体为参数： {}", JSONObject.toJSONString(sheetInfoList));
        //3、返回结果
        log.debug("询报价订单同步修改接口保存实体为：{}", JSONObject.toJSONString(sheetInfoList));
        try {
            int result = xeVenInquirysheetService.batchUpdateInquiry(sheetInfoList);
            if (result > 0) {
                responseDTO.setMsg("修改询价单成功");
                responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
            }
        } catch (Exception e) {
            log.info("修改询价单异常", e);
            responseDTO.setMsg("修改询价单异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return responseDTO;
    }

    /**
     * 根据询价单编号查询询价单信息
     *
     * @author Wangyp
     */
    @RequestMapping("/sheetInfoByCode")
    public XeVeInquireSheetResponseDTO sheetInfoByCode(@RequestBody XeVeInquireSheetRequestDTO paramDto) {
        XeVeInquireSheetResponseDTO dto = new XeVeInquireSheetResponseDTO();
        try {
            String formJson = JSONObject.toJSONString(paramDto);
            log.info("询价单查询条件：{}", formJson);
            // 查询条件封装
            XeVenInquirySheetEntity param = JSONObject.parseObject(formJson, XeVenInquirySheetEntity.class);
            // 根据询价单号查询询价单信息
            XeVenInquirySheetView result = xeVenInquirysheetService.queryXeVenInquiryByCode(param);
            XeVeInquireSheetResponseDTO salesBillList = JSONObject.parseObject(JSONArray.toJSONString(result),
                    XeVeInquireSheetResponseDTO.class);
            dto.setSheet(salesBillList);
            dto.setStatus(XeErrorCode.PORJECT_SUCCESS);
        } catch (Exception e) {
            log.error("商户应付款账单接口异常：异常信息{}", e);
            dto.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return dto;
    }


    /**
     * 同步询价单状态接口
     *
     * @author Wangyp
     */
    @RequestMapping("/updateStatus")
    public BaseResponseDTO batchUpdateSheet(@RequestBody XeVeInquireSheetRequestDTO dto) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        String peramJson = JSONObject.toJSONString(dto);
        log.info("同步询价单状态接口传入参数： {}", peramJson);
        //1、验证参数

        //2、拼装参数
        XeVenInquirySheetForm sheetForm = JSON.parseObject(peramJson, XeVenInquirySheetForm.class);
        //3、返回结果
        try {
            int result = xeVenInquirysheetService.batchUpdateSheet(sheetForm);
            if (result > 0) {
                responseDTO.setMsg("修改询价单状态成功");
                responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
            }
        } catch (Exception e) {
            log.info("修改询价单状态异常", e);
            responseDTO.setMsg("修改询价单状态异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return responseDTO;
    }


}
  
